package com.ui;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

public class ProcessSchedulerUI extends Application {
    private static int Height = 800;
    private static int Width = 950;
    private static Label processOutputLabel;
    private static Label readyQueueLabel;
    private static Label pcbListLabel;
    private static Label memoryAllocationLabel;
    private static Label managementLabel;
    private static Label AccessingVariablesLabel;
    private static TextFlow readyQueueTextFlow;
    private static TextFlow processOutputTextFlow;
    private static TextFlow pcbListTextFlow;
    private static TextFlow memoryAllocationFlow;
    private static TextFlow AccessingVariablesFlow;
    private static TextFlow managementFlow;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Process Scheduler");

        // 创建就绪队列信息
        readyQueueLabel = new Label("就绪列表信息：");
        readyQueueTextFlow = new TextFlow();
        readyQueueTextFlow.setPadding(new Insets(10));

        ScrollPane readyQueueScrollPane = new ScrollPane(readyQueueTextFlow);
        readyQueueScrollPane.setPrefSize(400, 250);
        readyQueueScrollPane.setFitToWidth(true);
        readyQueueScrollPane.setFitToHeight(true); // 设置ScrollPane自适应高度
        readyQueueScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条

        // 创建左部分的进程输出信息
        processOutputLabel = new Label("进程详细信息：");
        processOutputTextFlow = new TextFlow();
        processOutputTextFlow.setPadding(new Insets(10));

        // 创建一个ScrollPane来包装进程输出信息的TextFlow
        ScrollPane processOutputScrollPane = new ScrollPane(processOutputTextFlow);
        processOutputScrollPane.setPrefSize(400, 350);
        processOutputScrollPane.setFitToWidth(true);
        processOutputScrollPane.setFitToHeight(true); // 设置ScrollPane自适应高度
        processOutputScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条

        // 创建PCB列表信息
        pcbListLabel = new Label("PCB列表信息：");
        pcbListTextFlow = new TextFlow();
        pcbListTextFlow.setPadding(new Insets(10));

        // 创建一个ScrollPane来包装进程输出信息的TextFlow
        ScrollPane pcbListScrollPane = new ScrollPane(pcbListTextFlow);
        pcbListScrollPane.setPrefSize(400, 350);
        pcbListScrollPane.setFitToWidth(true);
        pcbListScrollPane.setFitToHeight(true); // 设置ScrollPane自适应高度
        pcbListScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条


        // 创建左边的父容器，使用VBox布局
        VBox leftContainer = new VBox();
        leftContainer.setSpacing(10);
        leftContainer.setPadding(new Insets(10));

        // 创建左边的控件
        leftContainer.getChildren().addAll(readyQueueLabel,readyQueueScrollPane,processOutputLabel,processOutputScrollPane,pcbListLabel,pcbListScrollPane);

        // 创建右边的内存分配情况信息
        memoryAllocationLabel = new Label("存贮器管理模块：");
        memoryAllocationFlow = new TextFlow();
        memoryAllocationFlow.setPadding(new Insets(10));

        ScrollPane memoryAllocationScrollPane = new ScrollPane(memoryAllocationFlow);
        memoryAllocationScrollPane.setPrefSize(450, 250);
        memoryAllocationScrollPane.setFitToWidth(true);
        memoryAllocationScrollPane.setFitToHeight(true); // 设置ScrollPane自适应高度
        memoryAllocationScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条

        AccessingVariablesLabel = new Label("虚拟存储器管理模块：");
        AccessingVariablesFlow = new TextFlow();
        AccessingVariablesFlow.setPadding(new Insets(10));

        ScrollPane AccessingVariablesScrollPane = new ScrollPane(AccessingVariablesFlow);
        AccessingVariablesScrollPane.setPrefSize(450, 300);
        AccessingVariablesScrollPane.setFitToWidth(true);
        AccessingVariablesScrollPane.setFitToHeight(true); // 设置ScrollPane自适应高度
        AccessingVariablesScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条

        managementLabel = new Label("文件管理模块：");
        managementFlow = new TextFlow();
        managementFlow.setPadding(new Insets(10));

        ScrollPane managementScrollPane = new ScrollPane(managementFlow);
        managementScrollPane.setPrefSize(450, 100);
        managementScrollPane.setFitToWidth(true);
        managementScrollPane.setFitToHeight(true); // 设置ScrollPane自适应高度
        managementScrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS); // 始终显示垂直滚动条

        // 创建右边的父容器，使用VBox布局
        VBox rightContainer = new VBox();
        rightContainer.setSpacing(10);
        rightContainer.setPadding(new Insets(10));

        // 创建左边的控件
        rightContainer.getChildren().addAll(memoryAllocationLabel,memoryAllocationScrollPane,AccessingVariablesLabel,AccessingVariablesScrollPane,managementLabel,managementScrollPane);


        // 创建中间留空的区域
        VBox centerBox = new VBox();
        centerBox.setPadding(new Insets(10));

        // 创建一个根容器，使用BorderPane布局
        BorderPane root = new BorderPane();
        root.setPadding(new Insets(10));
//        root.setTop(readyQueueScrollPane);
        root.setLeft(leftContainer);
        root.setCenter(centerBox);
        root.setRight(rightContainer);
//        root.setBottom(pcbListBox);

        // 创建一个Scene并将根容器添加到其中
        Scene scene = new Scene(root, Width, Height);

        // 监听窗口大小的变化，更新ScrollPane的内容缩放
        scene.widthProperty().addListener((observable, oldValue, newValue) -> {
            double scaleX = newValue.doubleValue() / Width; // 计算水平缩放比例
            double scaleY = newValue.doubleValue() / Height; // 计算垂直缩放比例
            Scale scale = new Scale(scaleX, scaleY);
            processOutputScrollPane.getTransforms().setAll(scale);
            readyQueueScrollPane.getTransforms().setAll(scale);
            pcbListScrollPane.getTransforms().setAll(scale);
        });
        scene.heightProperty().addListener((observable, oldValue, newValue) -> {
            double scaleX = newValue.doubleValue() / 800; // 计算水平缩放比例
            double scaleY = newValue.doubleValue() / 700; // 计算垂直缩放比例
            Scale scale = new Scale(scaleX, scaleY);
            processOutputScrollPane.getTransforms().setAll(scale);
            readyQueueScrollPane.getTransforms().setAll(scale);
            pcbListScrollPane.getTransforms().setAll(scale);
        });

        // 设置Scene并显示主舞台
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void updateProcessOutput(String output) {
        Platform.runLater(() -> {
            Text text = new Text(output + "\n");
            processOutputTextFlow.getChildren().add(text);
        });
    }

    public static void updateReadyQueue(String queue) {
        Platform.runLater(() -> {
            Text text = new Text(queue + "\n");
            readyQueueTextFlow.getChildren().add(text);
        });
    }

    public static void updatePCBList(String pcbList) {
        Platform.runLater(() -> {
            Text text = new Text(pcbList + "\n");
            pcbListTextFlow.getChildren().add(text);
        });
    }

    public static void updateMemoryAllocation(String allocation) {
        Platform.runLater(() -> {
            Text text = new Text(allocation + "\n");
            memoryAllocationFlow.getChildren().add(text);
        });
    }

    public static void updateAccessingVariables(String allocation) {
        Platform.runLater(() -> {
            Text text = new Text(allocation + "\n");
            AccessingVariablesFlow.getChildren().add(text);
        });
    }

    public static void updatemanagement(String allocation) {
        Platform.runLater(() -> {
            Text text = new Text(allocation + "\n");
            managementFlow.getChildren().add(text);
        });
    }
}